package com.yunquan.ohana.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.yunquan.ohana.utils.Utils

class CropOverlayView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    private val cropBoxPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#99000000")
        strokeWidth = 20f
        style = Paint.Style.STROKE
    }

    private val cropPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#FF202020")
        style = Paint.Style.FILL
    }

    private val cropCenterPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
        style = Paint.Style.FILL
    }

    private val notPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.parseColor("#80000000")
        style = Paint.Style.FILL
    }

    private val framePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.LTGRAY
        style = Paint.Style.FILL
    }

    private var lineWidth = 40
    private var centerLineMargin: Float = 42f
    private var thumbMargin = 0f
    private var firstX = 0f
    private var lastX = 0f
    private var isLeftDragging = false
    private var isRightDragging = false
    private var leftRect = Rect()
    private var rightRect = Rect()

    private var videoFrames: List<Bitmap> = emptyList()

    private var onCropRangeChangedListener: ((Float, Float) -> Unit)? = null
    private var onDragListener: ((Boolean) -> Unit)? = null

    init {
        centerLineMargin = Utils.instance.dp2px(21f).toFloat()
    }

    fun setOnCropRangeChangedListener(listener: (Float, Float) -> Unit) {
        this.onCropRangeChangedListener = listener
    }

    fun setOnDragListener(listener: (Boolean) -> Unit) {
        this.onDragListener = listener
    }

    fun setThumbMargin(margin: Float) {
        thumbMargin = margin
    }

    fun setVideoFrames(frames: List<Bitmap>) {
        videoFrames = frames
        invalidate()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 绘制视频帧
        if (videoFrames.isNotEmpty()) {
            val frameWidth = width / videoFrames.size
            videoFrames.forEachIndexed { index, bitmap ->
                val left = index * frameWidth
                val top = 0
                val right = (index + 1) * frameWidth
                val bottom = height
                val rect = Rect(left, top, right, bottom)
                canvas.drawBitmap(bitmap, null, rect, framePaint)
            }
        }
        if (lastX == 0f) {
            lastX = width.toFloat()
        }
        // 绘制选择框
        val selectRect = Rect((firstX + 10).toInt(), 0, (lastX - 10).toInt(), height)
        canvas.drawRect(selectRect, cropBoxPaint)

        // 绘制滑块
        leftRect = Rect(firstX.toInt(), 0, (firstX + lineWidth).toInt(), height)
        canvas.drawRect(leftRect, cropPaint)
        rightRect = Rect((lastX - lineWidth).toInt(), 0, lastX.toInt(), height)
        canvas.drawRect(rightRect, cropPaint)

        // 绘制滑块把手
        val leftLine = RectF(
            (firstX + lineWidth / 2) - 5,
            centerLineMargin,
            (firstX + lineWidth / 2) + 5,
            height - centerLineMargin
        )
        canvas.drawRoundRect(leftLine, 10f, 10f, cropCenterPaint)
        val rightLine = RectF(
            (lastX - lineWidth / 2) - 5,
            centerLineMargin,
            (lastX - lineWidth / 2) + 5,
            height - centerLineMargin
        )
        canvas.drawRoundRect(rightLine, 10f, 10f, cropCenterPaint)

        if (leftRect.left > 0) {
            val notRectLeft = Rect(0, 0, leftRect.left, height)
            canvas.drawRect(notRectLeft, notPaint)
        }
        if (rightRect.right < width) {
            val notRectRight = Rect(rightRect.right, 0, width, height)
            canvas.drawRect(notRectRight, notPaint)
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                if (leftRect.contains(event.x.toInt(), event.y.toInt())) {
                    isLeftDragging = true
                    firstX = event.x
                } else if (rightRect.contains(event.x.toInt(), event.y.toInt())) {
                    isRightDragging = true
                    lastX = event.x
                }
            }

            MotionEvent.ACTION_MOVE -> {
                if (isLeftDragging) {
                    firstX = event.x
                    if (firstX > (lastX - thumbMargin)) {
                        firstX = lastX - thumbMargin
                    } else if (firstX < 0f) {
                        firstX = 0f
                    }
                } else if (isRightDragging) {
                    lastX = event.x
                    if (lastX > width) {
                        lastX = width.toFloat()
                    } else if (lastX < (firstX + thumbMargin)) {
                        lastX = firstX + thumbMargin
                    }
                }
                if (isLeftDragging || isRightDragging) {
                    invalidate()
                    val leftDiff = firstX / width
                    val rightDiff = lastX / width
                    onCropRangeChangedListener?.invoke(leftDiff, rightDiff)
                }
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                isLeftDragging = false
                isRightDragging = false
                onDragListener?.invoke(false)
            }
        }
        return true
    }

}


